//
//  MyProfitViewController.m
//  NewPro
//
//  Created by 何荧红 on 2018/10/11.
//  Copyright © 2018 何荧红. All rights reserved.
//

#import "MyProfitViewController.h"
#import "MypromoteVC.h"
#import "SSWLineChartView.h"
#import "MyProfitModel.h"
#import "MyProfitCell.h"
#import "MyProfitFiveCell.h"
#import "MyProfitStore.h"
#import "MyhistoryProfit.h"

@interface MyProfitViewController ()<UITableViewDelegate,UITableViewDataSource,SSWChartsDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ViewHeader *header;
@property (nonatomic, strong) MyProfitModel *MyModel;

@property (nonatomic, strong) NSMutableArray *mutArr;
@end

@implementation MyProfitViewController

- (NSMutableArray *)mutArr{
    if (!_mutArr) {
        _mutArr = [[NSMutableArray alloc]init];
    }
    return _mutArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self initCSS];
}
- (void)loadData{
    [[[MyProfitStore alloc]init] GetMyProfitWithID:[UserDefaultsManager sharedInstance].user_id Success:^(NSDictionary * _Nonnull res) {
        if ([res[@"msg"] isEqualToString:@"success"]) {
            NSDictionary *arr = res[@"data"];
            if (arr.count > 1) {
                self.MyModel = [[MyProfitModel alloc]init];
                [self.MyModel setValuesForKeysWithDictionary:arr];
                self.header.monthmoney = self.MyModel.Monthsy;
                [self Cleandate];
            }
        }else{
            [self showMBPError:res[@"msg"]];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } Failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self showMBPError:[HttpTool handleError:error]];
    }];
}
- (void)Cleandate{
    NSMutableArray *subarr = [[NSMutableArray alloc]init];
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM"];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *lastMonthComps = [[NSDateComponents alloc] init];
//    [lastMonthComps setYear:1]; // year = 1表示1年后的时间 year = -1为1年前的日期
    NSMutableArray *montharr = [[NSMutableArray alloc]init];
    NSMutableArray *moneyarr = [[NSMutableArray alloc]init];
    for (NSInteger i = 4; i >= 0; i--) {
        [lastMonthComps setMonth:-i];
        NSDate *newdate = [calendar dateByAddingComponents:lastMonthComps toDate:currentDate options:0];
        NSString *dateStr = [formatter stringFromDate:newdate];
        if (i == 0) {
            [montharr addObject:dateStr];
            [moneyarr addObject:self.MyModel.Monthsy];
        }else if (i == 1){
            [montharr addObject:dateStr];
            [moneyarr addObject:self.MyModel.Monthsy1];
        }else if (i == 2){
            [montharr addObject:dateStr];
            [moneyarr addObject:self.MyModel.Monthsy2];
        }else if (i == 3){
            [montharr addObject:dateStr];
            [moneyarr addObject:self.MyModel.Monthsy3];
        }else if (i == 4){
            [montharr addObject:dateStr];
            [moneyarr addObject:self.MyModel.Monthsy4];
        }
    }
    [subarr addObject:montharr];
    [subarr addObject:moneyarr];
    self.MyModel.subarr = subarr;
}

- (void)onRightBtn{
    
    MyhistoryProfit *VC = [[MyhistoryProfit alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}

- (void)setNavBar{
    
    [self setNavBarWithTitle:@"我的收益"];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //修改按钮向左偏移10 point
    [rightBtn setFrame:CGRectMake(10.0, 0.0, 65.0, 44.0)];
    [rightBtn addTarget:self action:@selector(onRightBtn) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setTitle:@"历史收益" forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    //修改方法
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 65.0, 44.0)];
    [view addSubview:rightBtn];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    
    self.navigationItem.rightBarButtonItem = rightItem;
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}

- (void)initCSS{
    
    [self setNavBar];
    self.header = [[ViewHeader alloc]initWithMyProfit];
    [self.view addSubview:_header];
    [self.header mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(162);
        make.left.right.top.equalTo(self.view);
    }];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.header.mas_bottom);
    }];
    
    for (NSInteger i = 0; i < 5; i++) {
        MyProfitpush *model = [[MyProfitpush alloc]init];
        model.CllPush = @"0";
        [self.mutArr addObject:model];
    }
}
- (NSInteger)foundWith:(NSArray *)allarr{
    NSInteger subValue = 0;
    for (NSInteger i = 0; i < allarr.count; i++) {
        NSInteger maxvalue = [allarr[i] integerValue];
        if (maxvalue > subValue) {
            subValue = maxvalue;
        }
    }
    NSInteger subDValue = 0;
    for (NSInteger i = 0; i < allarr.count; i++) {
        NSInteger minvalue = [allarr[i] integerValue];
        if (minvalue < subValue) {
            subDValue = minvalue;
        }
    }
    if (subDValue < 0) {
        subValue = (subValue - subDValue) + (subValue - subDValue)/5;
    }else{
        subValue = subValue + subValue/5;
    }
    if (subValue/5 == 0) {
        return 2;
    }
    return subValue/5;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    if (section == 1) {
        return 1;
    }
    if (section == 2) {
        return 5;
    }
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 180;
    }
    if (indexPath.section == 1) {
        MyProfitpush *model = self.mutArr[indexPath.row];
        if ([model.CllPush isEqualToString:@"0"]) {
            return 45;
        }
        if ([model.CllPush isEqualToString:@"1"]) {
            return 109;
        }
    }
    if (indexPath.section == 2) {
        if (indexPath.row == 4) {
            return 45;
        }else{
            MyProfitpush *model = self.mutArr[indexPath.row + 1];
            if ([model.CllPush isEqualToString:@"0"]) {
                return 45;
            }
            if ([model.CllPush isEqualToString:@"1"]) {
                return 109;
            }
        }
    }
    return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 40;
    }else if (section == 1){
        return 72;
    }else{
        return 32;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        ViewHeader *header = [[ViewHeader alloc]initWithMyProfitTableHeader:0];
        return header;
    }else if(section == 1){
        ViewHeader *header = [[ViewHeader alloc]initWithMyProfitTableHeader:1];
        return header;
    }else{
        ViewHeader *header = [[ViewHeader alloc]initWithMyProfitTableHeader:2];
        return header;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return 10;
    }else if (section == 1){
        return 10;
    }else{
        return 65;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 0) {
        UIView *foot = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
        foot.backgroundColor = [UIColor colorWithHexString:@"F5F5F5"];
        return foot;
    }else if (section == 1) {
        UIView *foot = [[UIView alloc]initWithFrame:CGRectMake(0, -1, SCREEN_WIDTH, 10)];
        foot.backgroundColor = [UIColor colorWithHexString:@"F5F5F5"];
        return foot;
    }else{
        UIView *foot = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 65)];
        foot.backgroundColor = [UIColor colorWithHexString:@"F5F5F5"];
        ViewFoot *subfoot = [[ViewFoot alloc]initWithMyProfitFoot];
        [foot addSubview:subfoot];
        return foot;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = nil;
    if (indexPath.section == 0) {
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
            if (self.MyModel) {
                SSWLineChartView * lineChartView = [[SSWLineChartView alloc] initWithChartType:SSWChartsTypeLine];
                lineChartView.backgroundColor = [UIColor whiteColor];
                lineChartView.xValuesArr = self.MyModel.subarr[0];
                lineChartView.yValuesArr = self.MyModel.subarr[1];
//            lineChartView.unit = @"吨";
                lineChartView.barWidth = 77;
                lineChartView.yAxisCount = 5;
                lineChartView.yScaleValue = [self foundWith:self.MyModel.subarr[1]];
                lineChartView.lineColor = [UIColor colorWithHexString:@"0CA6F0"];
                lineChartView.delegate = self;
                lineChartView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 180);
                [cell.contentView addSubview:lineChartView];
            }
        }
    }
    if (indexPath.section == 1) {
        [self.tableView registerClass:[MyProfitCell class] forCellReuseIdentifier:@"MyProfitCellID"];
        MyProfitCell *PCell = [tableView dequeueReusableCellWithIdentifier:@"MyProfitCellID"];
        if (indexPath.row == 0) {
            PCell.rightArrow = NO;
            PCell.typelab.text = @"激活奖励";
        }
        [PCell setModel:self.MyModel With:indexPath.row];
        MyProfitpush *model = self.mutArr[indexPath.row];
        PCell.cllpush = model;
        [PCell setCellPushBlock:^(BOOL push) {
            MyProfitpush *inmodel = self.mutArr[indexPath.row];
            if (push) {
                inmodel.CllPush = @"1";
            }else{
                inmodel.CllPush = @"0";
            }
            [self.tableView reloadData];
        }];
        cell = PCell;
    }
    if (indexPath.section == 2) {
        if (indexPath.row == 4) {
            [self.tableView registerClass:[MyProfitFiveCell class] forCellReuseIdentifier:@"MyProfitFiveCellID"];
            MyProfitModel *model = self.MyModel;
            MyProfitFiveCell *FCell = [tableView dequeueReusableCellWithIdentifier:@"MyProfitFiveCellID"];
            FCell.addString = model.adjMoney;
            cell = FCell;
        }else{
            [self.tableView registerClass:[MyProfitCell class] forCellReuseIdentifier:@"MyProfitCellID"];
            MyProfitCell *PCell = [tableView dequeueReusableCellWithIdentifier:@"MyProfitCellID"];
            if (indexPath.row == 0) {
                PCell.rightArrow = NO;
                PCell.footbutton.enabled = NO;
                PCell.typelab.text = @"拓展奖励";
            }else if (indexPath.row == 1) {
                PCell.rightArrow = YES;
                PCell.footbutton.enabled = YES;
                PCell.typelab.text = @"推广奖励";
                [PCell setFootbutBlock:^{
                    [[AppDelegate sharedAppDelegate] pushViewController:[[MypromoteVC alloc]init]];
                }];
            }else if (indexPath.row == 2) {
                PCell.rightArrow = NO;
                PCell.footbutton.enabled = NO;
                PCell.typelab.text = @"团建奖励";
            }else if (indexPath.row == 3) {
                PCell.rightArrow = NO;
                PCell.footbutton.enabled = NO;
                PCell.typelab.text = @"管理奖励";
            }
            [PCell setModel:self.MyModel With:indexPath.row + 1];
            MyProfitpush *model = self.mutArr[indexPath.row + 1];
            PCell.cllpush = model;
            [PCell setCellPushBlock:^(BOOL push) {
                MyProfitpush *inmodel = self.mutArr[indexPath.row + 1];
                if (push) {
                    inmodel.CllPush = @"1";
                }else{
                    inmodel.CllPush = @"0";
                }
                [self.tableView reloadData];
            }];
            cell = PCell;
        }
    }
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.selected = UITableViewCellSelectionStyleNone;
    return cell;
}

@end
